import MBSocketProvider from './mb_socket_provider'

import MBQoS4ReciveDaemon from './mb_daemon_qos_recieve'
import MBQoS4SendDaemon from './mb_daemon_qos_send'
import MBAutoReLoginDaemon from './mb_daemon_auto_relogin'
import MBKeepAliveDaemon from './mb_daemon_keep_alive'

class MBCore {
    _socketEvenCallbacks: any = {};
    _websocketUrl: string = "";
    DEBUG: boolean = false;
    autoReLogin: boolean = true;
    connectedToServer: boolean = true;
    loginHasInit: boolean = false;
    currenetLoginInfo = {
        loginUserId: null,
        loginToken: null,
        loginExtra: null,
        firstLoginTime: 0
    }

    setWebsocketUrl(wsUrl: any) {
        this._websocketUrl = wsUrl;
    };

    getWebsocketUrl() {
        return this._websocketUrl;
    };


    isLoginHasInit() {
        return this.loginHasInit;
    };

    setLoginHasInit(loginHasInit: boolean) {
        this.loginHasInit = loginHasInit;
    }

    setAutoRelogin(autoRelogin: boolean) {
        this.autoReLogin = autoRelogin;
    };

    isAutoRelogin() {
        return this.autoReLogin;
    };

    isConnectedToServer() {
        return this.connectedToServer;
    }

    setConnectedToServer(connectedToServer: any) {
        this.connectedToServer = connectedToServer;
    };

    getCurrentLoginInfo() {
        return this.currenetLoginInfo;
    };

    setCurrenetLoginInfo(loginInfo: any) {
        this.currenetLoginInfo = loginInfo;
    };

    setCurrenetLoginInfo_firstLoginTime(firstLoginTime: any) {
        this.currenetLoginInfo.firstLoginTime = firstLoginTime;
    };

    getEventCallback(eventName: any) {
        if (eventName) {
            let callback = this._socketEvenCallbacks[eventName];
            if (callback) {
                return callback;
            }
            return null;
        }
    };

    on(eventName: any, callback: any) {
        if (eventName && callback) {
            // 如果存在则直接覆盖（替换）成最新的
            this._socketEvenCallbacks[eventName] = callback;
        }
    }
    emit(eventName: any, data: any) {
        var callbackForEvent = this.getEventCallback(eventName);
        if (callbackForEvent) {
            callbackForEvent(data);
        }
    };

    debugEnabled() {
        return this.DEBUG;
    };

    setDebugEnabled(enabled: any) {
        this.DEBUG = enabled;
    };

    release() {

        // 设置是否正常连接（登陆）到服务器的标识
        // （注意：在要closeLocalSocket前调用哦，否则将错误地触发自动重连逻辑）
        this.setConnectedToServer(false);

        // 尝试关闭本地Socket
        MBSocketProvider.closeLocalSocket();
        // 尝试停掉掉线重连线程（如果线程正在运行的话）
        MBAutoReLoginDaemon.stop();
        // 尝试停掉QoS质量保证（发送）心跳线程
        MBQoS4SendDaemon.stop();
        // 尝试停掉Keep Alive心跳线程
        MBKeepAliveDaemon.stop();
//		// 尝试停掉消息接收者
//		LocalUDPDataReciever.getInstance().stop();
        // 尝试停掉QoS质量保证（接收防重复机制）心跳线程
        MBQoS4ReciveDaemon.stop();

        // 并清除QoS发送队列缓存：防止不退出APP时切换另一账号后qos的缓存队列未清空
        MBQoS4SendDaemon.clear();
        // 并清除QoS接收队列缓存：防止不退出APP时切换另一账号后qos的缓存队列未清空
        MBQoS4ReciveDaemon.clear();

        // 清除事件回调列表
        // this._socketEvenCallbacks = {};

        this.setLoginHasInit(false);
        // this.setConnectedToServer(false);
        this.setCurrenetLoginInfo(null);
    }
}

export default new MBCore();